﻿using System;
using Foundation;
using hotfix;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class AlertImp : IPopManager
    {
        public void ShowConfirm(string msg, Action<bool> onSelect, string[] btnTexts = null)
        {
            throw new NotImplementedException();
        }

        public void ShowMsg(string msg)
        {
            UIViewController vcRoot = UIApplication.SharedApplication.KeyWindow.RootViewController;

            var root = vcRoot.View;
            var tag = 923857;
            UIView lastView = root.ViewWithTag(tag);
            if (lastView != null)
                lastView.RemoveFromSuperview();

            var viewBack = new UIView();
            viewBack.BackgroundColor = UIColor.Black.ColorWithAlpha(0.6f);
            viewBack.Tag = tag;
            viewBack.SetParent(root);
            viewBack.MakeConstraints(m =>
            {
                m.Bottom.EqualTo(root.Bottom()).Offset(-30);
                m.CenterX.EqualTo(root);
                m.Width.EqualTo(root).MultipliedBy(0.7f);
                // m.Height.EqualTo(NSObject.FromObject(100));
            });
            viewBack.Layer.CornerRadius = 10;

            UILabel labMsg = new UILabel();
            labMsg.SetParent(viewBack);
            labMsg.MakeConstraints(m =>
            {
                m.Left.EqualTo(viewBack).Offset(10);
                m.Right.EqualTo(viewBack).Offset(-10);
                m.Top.EqualTo(viewBack).Offset(10);
                m.Bottom.EqualTo(viewBack).Offset(-10);
            });
            labMsg.Lines = 2;
            labMsg.Text = msg;
            labMsg.TextColor = UIColor.White;
            labMsg.TextAlignment = UITextAlignment.Center;

            UIView.BeginAnimations("Toast");
            UIView.SetAnimationDuration(2.0f);
            viewBack.Alpha = 0.0f;
            UIView.CommitAnimations();

        }

        public void ShowMsgbox(string msg)
        {
            throw new NotImplementedException();
        }

        public void ShowOK2(string msg)
        {
            var logger = ServiceLocator.Instance.Get<ILog>();
            var global = ServiceLocator.Instance.Get<IGlobal>() as GlobalImpl;

            UIViewController uic = UIApplication.SharedApplication.KeyWindow.RootViewController;

            var okAlertController = UIAlertController.Create("Title", "The message", UIAlertControllerStyle.ActionSheet);

            // okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, a =>
            // {
            //     uic.DismissViewController(false, null);
            //     logger.Log("after dimiss alert");
            // }));

            // Present Alert
            uic.PresentViewController(okAlertController, true, null);
            logger.Log("after show alert");
            // global.InspectViews();
        }

        public void ShowToast(string msg)
        {
            throw new NotImplementedException();
        }

        public IPopWaiter ShowWait(string msg, int countDownSecond = 0)
        {
            throw new NotImplementedException();
        }
    }
}